﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using System.IO;
using System.Reflection.Emit;
using System.Xml.Linq;
namespace 玩具商城管理系统.znew
{
    public partial class pj : Form
    {
        public pj()
        {
            InitializeComponent();
        }
        SqlDataAdapter dahot;
        DataSet ds = new DataSet();
        public static string aa = "";
        public static string path_source = "";
        void init()//初始化
        {
            DB.GetCn();
            string str = "select * from orderT where orderT_cid='" + login.username + "'and orderT_check='未审核'";
            dahot = new SqlDataAdapter(str, DB.cn);
            dahot.Fill(ds, "hot_info");
        }
        void showAll()
        {
            DB.GetCn();
            string str = "select * from orderT where orderT_cid='" + login.username + "'and orderT_check='未审核'";
            SqlCommand cmd = new SqlCommand(str, DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                int index = dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells[0].Value = rdr[0];
                dataGridView1.Rows[index].Cells[1].Value = rdr[8];
                dataGridView1.Rows[index].Cells[2].Value = rdr[12];
                try
                {
                    Image imageColumn = Image.FromFile(Application.StartupPath + rdr[13]);
                    dataGridView1.Rows[index].Cells["Column4"].Value = imageColumn;
                }
                catch
                {
                    Image imgeColumn = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg");
                    dataGridView1.Rows[index].Cells["Column4"].Value = imgeColumn;
                }
            }
            rdr.Close();
        }
     
        private void pj_Load(object sender, EventArgs e)
        {
            dataGridView1.RowTemplate.Height = 80;
            init();
            showAll();
            dataGridView1.Columns[0].Width = 100;
            dataGridView1.Columns[1].Width = 100;
            dataGridView1.Columns[2].Width = 200;
            dataGridView1.Columns[3].Width = 80;
            dataGridView1.Columns[0].HeaderText = "订单编号";
            dataGridView1.Columns[1].HeaderText = "订单状态";
            dataGridView1.Columns[2].HeaderText = "退货理由";
            dataGridView1.Columns[3].HeaderText = "凭证图片";


        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (label3.Text == "")
                {
                    MessageBox.Show("尊敬的客户请你选择你要退货的订单");
                }
                else if (label5.Text!="")
                {
                    MessageBox.Show("已经发了退货申请请勿重复提交");
                }
                else if ( path_source==""|| textBox1.Text == "")
                {
                    MessageBox.Show("退货原因和凭证图片都要填写才可以提交哦");
                }
                else
                {
                    DB.GetCn();
                    string filename;
                    string fileFolder;
                    string dateTime = "";
                    filename = Path.GetFileName(path_source);
                    dateTime += DateTime.Now.Year.ToString();
                    dateTime += DateTime.Now.Month.ToString();
                    dateTime += DateTime.Now.Day.ToString();
                    dateTime += DateTime.Now.Hour.ToString();
                    dateTime += DateTime.Now.Minute.ToString();
                    dateTime += DateTime.Now.Second.ToString();
                    filename = dateTime + filename;
                    fileFolder = Directory.GetCurrentDirectory() + "\\" + "Prod_Images\\" + "退货" + "\\";
                    fileFolder += filename;
                    File.Copy(path_source, fileFolder, true);
                    string a1 = "\\Prod_Images\\" + "退货" + "\\" + filename;
                    string a2 = textBox1.Text;
                    string str2 = "update orderT set orderT_th='" + a2 + "',orderT_img='" + a1 + "' where orderT_id='" + label3.Text + "'";
                    DB.sqlEx(str2);
                    MessageBox.Show("退货申请已发送");
                    dataGridView1.Rows.Clear();
                    showAll();

                
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path_source = ofd.FileName;
                pictureBox1.Image = Image.FromFile(path_source);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
                label3.Text = dataGridView1.CurrentRow.Cells["Column1"].Value.ToString();
            label5.Text = dataGridView1.CurrentRow.Cells["Column3"].Value.ToString();
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
